<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link href="assets/css/bootstrap.min.css" rel="stylesheet" />
    <link rel="stylesheet" href="css/style.css"/>
    <link rel="stylesheet" href="assets/css/ace.min.css" />
    <link rel="stylesheet" href="assets/css/font-awesome.min.css" />
    <link href="Widget/icheck/icheck.css" rel="stylesheet" type="text/css" />
    <script src="js/jquery-1.9.1.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
    <script src="assets/js/typeahead-bs2.min.js"></script>
    <script src="assets/js/jquery.dataTables.min.js"></script>
    <script src="assets/js/jquery.dataTables.bootstrap.js"></script>
    <script src="assets/layer/layer.js" type="text/javascript" ></script>
    <script src="assets/laydate/laydate.js" type="text/javascript"></script>
    <script type="text/javascript" src="js/H-ui.js"></script>
    <script type="text/javascript" src="js/H-ui.admin.js"></script>
    <script src="js/lrtk.js" type="text/javascript"></script>

    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.js"></script>
    <title>分类管理</title>
</head>

<body>
<div id="app" class="page-content clearfix" >


        <!----------------------------------分类操作------------------------------->
        <div class="border clearfix">
           <span class="l_f">
    <!-----------------------添加分类，弹出一个弹窗添加-------------------------->
            <button @click="addType" class="btn btn-warning Order_form"><i class="icon-plus"></i>添加耗材类型</button>
            <a href="#" class="btn btn-danger"><i class="icon-trash"></i>批量删除</a>
           </span>
            查找：<input id="sel" placeholder="输入关键字" type="text">
            <button @click="selectType" class="btn btn_search"><i class="icon-search"></i>查询</button>
            <button @click="resetType" class="btn btn-danger">重置</button>
            每页显示行数：
            <select @change="initData(1)" v-model="pageSize">
                <option v-for="size in sizes">{{size}}</option>
            </select>
        </div>
        <!-----------------------耗材列表------------------->
        <div class="b_products_list clearfix" id="products_list">
            <div id="scrollsidebar" class="left_Treeview">
                <div class="show_btn" id="rightArrow"><span></span></div>
                <div class="widget-box side_content" >
                    <div class="side_title"><a title="隐藏" class="close_btn"><span></span></a></div>
                    <div class="side_list">
                        <div class="widget-header header-color-green2"><h4 class="lighter smaller">所有分类</h4></div>
                        <div class="widget-body">
                            <ul class="b_P_Sort_list">
                                <li><i class="orange icon-folder-close"></i><a href="#">全部(2)</a></li>

                                <li><i class="icon-file-text grey"></i> <a href="#">铁件</a></li>
                                <li> <i class="icon-file-text grey"></i><a href="#">塑料件</a> </li>


                            </ul>

                        </div>
                    </div>
                </div>
            </div>
            <div v-if="showAdd">
                <table>
                    <tr>
                        <td>    故障分类名：<input type="text" ref="title" v-model.trim="addMaterialsType.title"
                                 placeholder="请输入15字以内中文">
            <span>{{err.title}}</span>
            </td>
            </tr>
            <tr>
                <td>
                    排序：<input type="text" ref="sort" v-model.trim="addMaterialsType.sort" placeholder="请输入数字">
                    <span>{{err.sort}}</span>
                </td>
            </tr>
            <tr>
                <td>
                    <button @click="save">提交</button>
                    <button @click="backTab">返回</button>
                </td>
            </tr>
            </table>
        </div>
            <div class="table_menu_list" id="testIframe">

                <!--列表展示-->
                <div v-if="showTab">
                    <table class="table table-striped table-bordered table-hover" id="sample-table">
                        <thead>
                        <tr>
                            <th width="25px"><label><input type="checkbox" class="ace"><span class="lbl"></span></label>
                            </th>
                            <th width="80px">分类ID</th>
                            <th width="250px">分类名称</th>
                            <td @click="sortTal">排序(点击改变顺序)</td>
                            <th width="200px">操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr v-for="materials,index in materialsTypes">
                            <td width="25px"><label><input type="checkbox" class="ace"><span class="lbl"></span></label>
                            </td>
                            <td width="80px">{{materials.id}}</td>
                            <td width="250px"><u style="cursor:pointer" class="text-primary" onclick="">{{materials.title}}</u>
                            </td>
                            <td>{{materials.sort}}</td>
                            <td>
                                <button @click="updateType(index)" class="btn btn-xs btn-info"><i
                                        class="icon-edit bigger-120"></i></button>
                                <button @click="delType(index)" class="btn btn-xs btn-warning"><i
                                        class="icon-trash  bigger-120"></i></button>
                            </td>
                        </tr>
                        </tbody>
                    </table>

                    <div class="row">
                        <div class="col-sm-6">
                            <div class="dataTables_info" id="sample-table_info" role="status" aria-live="polite">
                                第{{pageInfo.pageNum}}/{{pageInfo.pages}} 页，共 {{pageInfo.total}} 条
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="dataTables_paginate paging_bootstrap" id="sample-table_paginate">
                                <ul class="pagination">
                                    <li :class="{'disabled':pageInfo.pageNum==1}">
                                        <a href="#" @click="initData(pageInfo.prePage)">上一页</a>
                                    </li>

                                    <li v-for="i in pageInfo.pages" @click.prevent="initData(i)"
                                        :class="{'active':pageInfo.pageNum==i}">
                                        <a href="#"> {{i}}</a>
                                    </li>

                                    <li :class="{'disabled':pageInfo.pageNum==pageInfo.pages}">
                                        <a :class="{'disabled':pageInfo.pageNum==pageInfo.pages}"
                                           href="#" @click="initData(pageInfo.nextPage)">下一页</a>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>


    </div>
</div>
</body>
</html>
<script>
    var app = new Vue({
        el: "#app",
        data: {
            materialsTypes: [],//存放后端的数据
            pageInfo: {},//分页数据
            sortAsc: true,//排序顺序
            showTab: true,//显示所有数据表格
            showAdd: false,//显示添加的输入框
            addMaterialsType: {id: 0},//存放新增加的数据
            sizes: [5, 10, 15, 20, 25, 30],
            pageSize: 5,
            err: {
                title: "",
                sort: ""
            },
            selectTitle: {
                title: "",
                id: 0,
            },
        },
        created: function () {
            this.initData(1);
        },
        methods: {
            //数据初始化
            initData: function (pageNum) {
                this.showTab = true;
                this.showAdd = false;
                axios.post("http://localhost:8080/manage/showMaterials?pageNum=" + pageNum + "&pageSize=" + this.pageSize, this.selectTitle)
                    .then(res => {
                        console.log(res.data.data);
                        this.materialsTypes = res.data.data.list;
                        this.pageInfo = res.data.data;
                    });
            },
            //排序控制
            sortTal: function () {
                this.materialsTypes.sort((a, b) => {
                    return this.sortAsc ? a.sort - b.sort : b.sort - a.sort;
                });
                this.sortAsc = !this.sortAsc;
            },
            //点击增加按键
            addType: function () {
                this.showAdd = true;
                this.showTab = false;
            },
            //返回表格
            backTab: function () {
                this.initData(this.pageInfo.pageNum);
                this.showAdd = false;
                this.showTab = true;
                this.addMaterialsType = {id: 0};
            },
            //保存数据
            save: function () {
                console.log(this.addMaterialsType);
                console.log(this.materialsTypes);
                var te = /^[\u4e00-\u9fa5_a-zA-Z0-9]{4,10}$/;
                var te1 = /^[0-9]{0,2}$/;
                var flag = false;
                if (this.addMaterialsType.title == undefined || this.addMaterialsType.title == "") {
                    this.err.title = "分类名不能为空";
                    this.$refs.title.focus();
                    return;
                } else if (!te.test(this.addMaterialsType.title)) {
                    this.err.title = "请输入4-10位汉字";
                    this.$refs.title.focus();
                    return;
                } else if (this.addMaterialsType.id != 0) {
                    console.log("2222");
                } else {
                    this.materialsTypes.forEach(MaterialsType => {
                        if (this.addMaterialsType.title == MaterialsType.title) {
                            this.err.title = "不能添加重复的故障分类";
                            this.$refs.title.focus();
                            flag = true;
                        }
                    });
                    if (flag) return;
                    this.err.title = "";
                }

                if (this.addMaterialsType.sort == undefined || this.addMaterialsType.sort == "") {
                    this.err.sort = "排序数值不能为空";
                    this.$refs.sort.focus();
                    return;
                } else {
                    if (!te1.test(this.addMaterialsType.sort)) {
                        this.err.sort = "请输入数字";
                        this.$refs.sort.focus();
                        return;
                    }
                    this.err.sort = "";
                }
                if (this.addMaterialsType.id != 0) {
                    axios.post("http://localhost:8080/manage/updateMaterialsType", this.addMaterialsType).then(
                        res => {
                            this.backTab();
                        }
                    );
                } else {
                    this.addMaterialsType.id = null;
                    axios.put("http://localhost:8080/manage/addMaterialsType", this.addMaterialsType).then(
                        res => {
                            console.log(res.data);
                            this.backTab();
                        });
                }
            },
            selectType: function () {
                console.log(document.getElementById("sel").value);

                this.selectTitle.title = document.getElementById("sel").value;
                this.initData(1);
            },
            resetType: function () {
                this.selectTitle.id = 0;
                this.selectTitle.title = "";
                this.initData(1)
            },
            delType: function (index) {
                axios.delete("http://localhost:8080/manage/deleteMaterialsType" + this.materialsTypes[index].id)
                    .then(res => {
                        this.materialsTypes.splice(index, 1);
                        this.initData(this.pageInfo.pageNum)
                    });

            },
            updateType: function (index) {
                this.showAdd = true;
                this.showTab = false;
                this.addMaterialsType = this.materialsTypes[index];
            },
        }
    });

</script>
<script type="text/javascript">
    //数据
    jQuery(function($) {
        var oTable1 = $('#sample-table').dataTable( {
            "aaSorting": [[ 1, "desc" ]],//默认第几个排序
            "bStateSave": true,//状态保存
            "aoColumnDefs": [
                {"orderable":false,"aTargets":[0,2,3,4,5,8,9]}// 制定列不参与排序
            ] } );


        $('table th input:checkbox').on('click' , function(){
            var that = this;
            $(this).closest('table').find('tr > td:first-child input:checkbox')
                .each(function(){
                    this.checked = that.checked;
                    $(this).closest('tr').toggleClass('selected');
                });

        });


        $('[data-rel="tooltip"]').tooltip({placement: tooltip_placement});
        function tooltip_placement(context, source) {
            var $source = $(source);
            var $parent = $source.closest('table')
            var off1 = $parent.offset();
            var w1 = $parent.width();

            var off2 = $source.offset();
            var w2 = $source.width();

            if( parseInt(off2.left) < parseInt(off1.left) + parseInt(w1 / 2) ) return 'right';
            return 'left';
        }
    });
    //图层隐藏限时参数
    $(function() {
        $("#products_list").fix({
            float : 'left',
            //minStatue : true,
            skin : 'green',
            durationTime :false,
            stylewidth:'220',
            spacingw:30,//设置隐藏时的距离
            spacingh:260,//设置显示时间距
        });
    });
    //初始化宽度、高度
    $(".widget-box").height($(window).height()-215);
    $(".table_menu_list").width($(window).width()-260);
    $(".table_menu_list").height($(window).height()-215);
    //当文档窗口发生改变时 触发
    $(window).resize(function(){
        $(".widget-box").height($(window).height()-215);
        $(".table_menu_list").width($(window).width()-260);
        $(".table_menu_list").height($(window).height()-215);
    });
    /*品牌-停用*/
    function member_stop(obj,id){
        layer.confirm('确认要停用吗？',function(index){
            $(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" class="btn btn-xs " onClick="member_start(this,id)" href="javascript:;" title="启用"><i class="icon-ok bigger-120"></i></a>');
            $(obj).parents("tr").find(".td-status").html('<span class="label label-defaunt radius">已停用</span>');
            $(obj).remove();
            layer.msg('已停用!',{icon: 5,time:1000});
        });
    }

    /*用户-启用*/
    function member_start(obj,id){
        layer.confirm('确认要启用吗？',function(index){
            $(obj).parents("tr").find(".td-manage").prepend('<a style="text-decoration:none" class="btn btn-xs btn-success" onClick="member_stop(this,id)" href="javascript:;" title="停用"><i class="icon-ok bigger-120"></i></a>');
            $(obj).parents("tr").find(".td-status").html('<span class="label label-success radius">已启用</span>');
            $(obj).remove();
            layer.msg('已启用!',{icon: 6,time:1000});
        });
    }

    /*品牌-删除*/
    function member_del(obj,id){
        layer.confirm('确认要删除吗？',function(index){
            $(obj).parents("tr").remove();
            layer.msg('已删除!',{icon:1,time:1000});
        });
    }
    //面包屑返回值
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.iframeAuto(index);
    $('.Order_form').on('click', function(){
        var cname = $(this).attr("title");
        var chref = $(this).attr("href");
        var cnames = parent.$('.Current_page').html();
        var herf = parent.$("#iframe").attr("src");
        parent.$('#parentIfour').html(cname);
        parent.$('#iframe').attr("src",chref).ready();;
        parent.$('#parentIfour').css("display","inline-block");
        parent.$('.parentIframe').attr("name",herf).css({"color":"#4c8fbd","cursor":"pointer"});
        parent.layer.close(index);

    });
</script>
